<?php
    
    include "../database.php";  
    
    if (isset($_GET['method'])){
           
        $fileString = "";
        //$file = $_POST['file'];
        //$file = $_FILES["file"]["tmp_name"];

        
        $data = array('success' => '1', 'content' => "lalala");
        echo json_encode($data);
        
        
        /*
        //if ($file != "" && file_exists($file)){
        if (file_exists("upload/" . $_FILES["file"]["name"])){
        
            $fh = fopen($file, 'r');
            //$fh = fopen($_FILES["file"]["tmp_name"], 'r');

            if ($fh != FALSE){

                while(!feof($fh)){
                    $fileString = $fileString.fgets($fh);
                    //echo fgets($fh);
                }

                fclose($fh);
            }
        }
        
                
        if ($fileString != "")
            $data = array('success' => '1', 'content' => $fileString);
        else
            $data = array('success' => '0');
        
        echo json_encode($data);*/
        
        
        
    } else if (isset($_POST['method_export'])){

        $content = $_POST['documentation'];
        $fileName = "Documentation.txt";
        
        $fp = fopen($fileName,"w"); 
        fwrite($fp, $content); 
        fclose($fp);

        $data = array('success' => '1');
        
        echo json_encode($data);
        
    }
    
    else{
    
        $project_id = $_POST['doc_project_id'];
        $content = $_POST['documentation'];    

        $queryFindDoc = "SELECT * FROM documentation WHERE documentation_project_id = '$project_id'";
        $resultFindDoc = executeQuery($queryFindDoc);
        $num_rows = mysql_num_rows($resultFindDoc);

        if ($num_rows < 1){
            $query = "INSERT INTO documentation (documentation_project_id, documentation_content) VALUES ('$project_id', '$content')";
            executeQuery($query);
        }else{
            $query = "UPDATE documentation SET documentation_content = '$content' WHERE documentation_project_id = '$project_id'";
            executeQuery($query);
        }     
        
       
        header("Location: ../project_wall.php?tab=2");
    
    }
?>
